﻿using GiteePlugin.Configuration;
using GiteePlugin.Features.Activity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Refit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GiteePlugin.Core.Extensions
{
    // 使用插件标准命名
    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection AddGiteePlugin(
            this IServiceCollection services,
            Action<GiteeConfig> configure)
        {
            services.Configure(configure);


            // 自动注册所有功能模块
            services.AddGiteeCore()
                    .AddActivities();
            //        .AddRepositories()
            //        .AddUsers()
            //        .AddIssues();

            return services;
        }

        /// <summary>
        /// 添加核心模块
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddGiteeCore(this IServiceCollection services)
        {
            services.AddScoped<GiteeClient>();
            return services;
        }


        /// <summary>
        /// 动态通知模块功能
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddActivities(this IServiceCollection services)
        {
            var giteeApiBaseAddress = new Uri("https://gitee.com/api");
            services
                .AddRefitClient<IActivityApi>()
                .ConfigureHttpClient((sp, client) =>
                {
                    var config = sp.GetRequiredService<IOptions<GiteeConfig>>().Value;
                    client.BaseAddress = new Uri(config.BaseUrl);
                });

            services.AddScoped<ActivityService>();
            return services;
        }
    }
}
